//
//  TLCommentMoreAlterView.m
//  WXReader
//
//  Created by huaya on 2025/5/12.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLCommentMoreAlterView.h"
#import "AppDelegate.h"
#import "TLCommentMoreTableViewCell.h"

@interface TLCommentMoreAlterView ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *jbBtn;
@property (weak, nonatomic) IBOutlet UIButton *lhBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConst;
@property (weak, nonatomic) IBOutlet UIView *btmView;
@property (weak, nonatomic) IBOutlet UIView *selectReasonView;
@property (nonatomic, strong) UITableView *tableView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *reasonBottomConst;
@property (weak, nonatomic) IBOutlet UIView *blockView;
@property (weak, nonatomic) IBOutlet UIButton *blockBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *blockCenterConst;
@property (nonatomic, strong) NSArray *reasonArr;

@property (nonatomic, assign) NSInteger commentId;
@end

@implementation TLCommentMoreAlterView

+ (instancetype)createViewWithCommentId:(NSInteger)comment_id{
    TLCommentMoreAlterView *view = [[[NSBundle mainBundle] loadNibNamed:@"TLCommentMoreAlterView" owner:nil options:nil] lastObject];
    view.commentId = comment_id;
    return view;
}
- (void)awakeFromNib{
    
    [super awakeFromNib];
    
    _btmView.layer.cornerRadius = 12;
    
    self.reasonArr = @[@"Spam information",
                       @"False information",
                       @"Abusive behavior, personal attacks, etc.",
                       @"Harmful information",
                       @"Alleged infringement",
                       @"Harass others",
    ];
    
    [self.selectReasonView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.selectReasonView.mas_left);
        make.right.equalTo(self.selectReasonView.mas_right);
        make.bottom.equalTo(self.selectReasonView.mas_bottom);
        make.top.equalTo(self.selectReasonView).offset(50);
    }];
    
    
    self.blockView.layer.cornerRadius = 8;
    self.blockBtn.layer.masksToBounds = YES;
    self.blockBtn.layer.cornerRadius = 47/2;
    
    _blockCenterConst.constant = kScreenHeight;
    
    UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [self hidAction];
    }];
    
    [self addGestureRecognizer:tapGest];
    
}
- (void)drawRect:(CGRect)rect{
    
    
    UIBezierPath *corner = [UIBezierPath bezierPathWithRoundedRect:self.selectReasonView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12.0, 12.0)];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = corner.CGPath;
    self.selectReasonView.layer.mask = layer;
    
}
- (void)showAction{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.window addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(delegate.window);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.3 animations:^{
            [self setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.6]];
            self.bottomConst.constant = 20+MK_SCREEN_SAVE_BOTTOM_HEIGHT;
            [self layoutIfNeeded];
        }];
    });
}
- (IBAction)blockCancelAction:(id)sender {
    [self hidAction];
}
- (IBAction)blockSureAction:(id)sender {
    [self returnActionWithType:1];
    [self hidAction];
}

- (void)hidAction{
    
    [UIView animateWithDuration:0.3 animations:^{
        [self setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.6]];
        self.bottomConst.constant = -136;
        self.reasonBottomConst.constant = -330;
        self.blockCenterConst.constant = 600;
        [self layoutIfNeeded];
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
- (IBAction)jubaoAction:(id)sender {
    
    [self showReasonView];
}
- (IBAction)labeiAction:(id)sender {
    
    [self showBlockView];
    
}
- (UITableView *)tableView{
    if (!_tableView) {
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width,MK_SCREEN_SAVE_BOTTOM_HEIGHT)];
        footView.backgroundColor = UIColor.clearColor;
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.tableFooterView = footView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = YES;
        if (@available(iOS 15.0, *)) {
            self.tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.frame = CGRectMake(0, 50, kScreenWidth, kScreenHeight+300);
        [_tableView registerNib:[UINib nibWithNibName:@"TLCommentMoreTableViewCell" bundle:nil] forCellReuseIdentifier:@"TLCommentMoreTableViewCell"];
    }
    return _tableView;
}
#pragma mark - table delegate - datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    TLCommentMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TLCommentMoreTableViewCell"];
    
    cell.titleLab.text = self.reasonArr[indexPath.row];
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 44;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.reasonArr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self returnActionWithType:0];
    [self hidAction];
    
}
- (void)showReasonView{
    
    [UIView animateWithDuration:0.3 animations:^{
        [self setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.6]];
        self.bottomConst.constant = -136;
        [self layoutIfNeeded];
    }];
    
    [UIView animateWithDuration:0.3 animations:^{
        [self setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.6]];
        self.reasonBottomConst.constant = 0;
        [self layoutIfNeeded];
    }];
}
- (void)showBlockView{
    
    [UIView animateWithDuration:0.3 animations:^{
        [self setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.6]];
        self.bottomConst.constant = -136;
        [self layoutIfNeeded];
    }];
    
    [UIView animateWithDuration:0.3 animations:^{
        [self setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.6]];
        self.blockCenterConst.constant = 0;
        [self layoutIfNeeded];
    }];
}
- (IBAction)closeReasonAction:(id)sender {
    [self hidAction];
}
- (void)returnActionWithType:(int)type{
    if (self.returnCommentIdBlock) {
        self.returnCommentIdBlock(self.commentId,type);
    }
}
@end
